package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) {
            try (Connection connection = DBUtil.getConnection();) {
                Scanner scanner = new Scanner(System.in);
                System.out.println("请输入用户名:");
                String username = scanner.nextLine();
                System.out.println("请输入密码:");
                String password = scanner.nextLine();
                Statement statement = connection.createStatement();
                String sql = "SELECT id,username,password,nickname,age\n" +
                        "    FROM user\n" +
                        "WHERE username = '" + username + "' AND password = '" + password + "'";
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next()) {
                    System.out.println("登录成功");
                    String userName = rs.getString("username");
                    String passWord = rs.getString("password");
                    String line = String.join(",", userName, passWord);
                    System.out.println(line);
                } else {
                    System.out.println("用户名或密码错误");
                    System.out.println("请重新输入");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }






    }
}
